package com.jy.modules.cims.component.quota;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.CimsIrtUrlConstant;
import com.jy.modules.cims.common.JResult;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.DateTimeUtils;
import com.jy.modules.cims.common.sysUtils.SearchParamUtil;
import com.jy.modules.cims.common.sysUtils.SimpleBeanUtils;
import com.jy.modules.cims.common.sysUtils.SimpleStringUtils;
import com.jy.modules.cims.component.common.service.TCMmCustinfoService;
import com.jy.modules.cims.component.common.service.TCMmQuotaLoanDetailService;
import com.jy.modules.cims.component.common.service.TCMmQuotaRetrunService;
import com.jy.modules.cims.component.creditRight.ICCreditRight;
import com.jy.modules.cims.component.loan.ICLoan;
import com.jy.modules.cims.component.sysAssist.BatchService;
import com.jy.modules.cims.component.sysAssist.BorrowersImpl;
import com.jy.modules.cims.component.sysAssist.ICSysAssist;
import com.jy.modules.cims.data.common.dao.TCMmContractConstraintDao;
import com.jy.modules.cims.data.common.dao.TCMmContractFeeDao;
import com.jy.modules.cims.data.common.dao.TCMmCooperorgDao;
import com.jy.modules.cims.data.common.dao.TCMmCreditPackageDao;
import com.jy.modules.cims.data.common.dao.TCMmImgurlDao;
import com.jy.modules.cims.data.common.dao.TCMmQuotaCreditDetailDao;
import com.jy.modules.cims.data.common.dto.TCMmContractConstraintDTO;
import com.jy.modules.cims.data.common.dto.TCMmContractFeeDTO;
import com.jy.modules.cims.data.common.dto.TCMmCooperorgDTO;
import com.jy.modules.cims.data.common.dto.TCMmCreditDetailDTO;
import com.jy.modules.cims.data.common.dto.TCMmCreditPackageDTO;
import com.jy.modules.cims.data.common.dto.TCMmCustinfoDTO;
import com.jy.modules.cims.data.common.dto.TCMmImgurlDTO;
import com.jy.modules.cims.data.common.dto.TCMmQuotaCreditDetailDTO;
import com.jy.modules.cims.data.common.dto.TCMmQuotaDTO;
import com.jy.modules.cims.data.common.dto.TCMmQuotaLoanDetailDTO;
import com.jy.modules.cims.data.common.dto.TCMmQuotaRetrunDTO;
import com.jy.modules.cims.data.dao.ImgurlDao;
import com.jy.modules.cims.data.dao.QuotaDao;
import com.jy.modules.cims.data.dto.CustInfoDto;
import com.jy.modules.cims.data.dto.ImgurlDto;
import com.jy.modules.cims.data.dto.QuotaApplyParamDto;
import com.jy.modules.cims.data.dto.QuotaApplyResultDto;
import com.jy.modules.cims.data.dto.QuotaCreditDetailDto;
import com.jy.modules.cims.data.dto.QuotaCreditRightDto;
import com.jy.modules.cims.data.dto.QuotaDetailUnlockParamDto;
import com.jy.modules.cims.data.dto.QuotaDto;
import com.jy.modules.cims.data.dto.QuotaQueryDto;
import com.jy.modules.cims.data.interact.core.loan.response.BorrowerAssetDTO;
import com.jy.modules.cims.data.interact.core.loan.response.BorrowerAttenDTO;
import com.jy.modules.cims.data.interact.core.loan.response.BorrowerBankCardDTO;
import com.jy.modules.cims.data.interact.core.loan.response.BorrowerBizEntityDTO;
import com.jy.modules.cims.data.interact.core.loan.response.BorrowerCreditInfoDTO;
import com.jy.modules.cims.data.interact.core.loan.response.BorrowerDTO;
import com.jy.modules.cims.data.interact.core.loan.response.BorrowerInsuranceDTO;
import com.jy.modules.cims.data.interact.core.loan.response.BorrowerJobsDTO;
import com.jy.modules.cims.data.interact.core.loan.response.BorrowerLoandebtDTO;
import com.jy.modules.cims.data.interact.core.loan.response.BorrowerThirdDataDTO;
import com.jy.modules.cims.data.interact.core.loan.response.FeesData;
import com.jy.modules.cims.data.interact.core.loan.response.ImgUrl;
import com.jy.modules.cims.data.interact.core.loan.response.LoanData;
import com.jy.modules.cims.data.interact.core.loan.response.LoanInfo;
import com.jy.modules.cims.data.interact.core.loan.response.Response2011DTO;
import com.jy.modules.cims.data.interact.core.loan.response.Response8003DTO;
import com.jy.modules.cims.data.interact.core.loan.response.UrlData;
import com.jy.modules.cims.data.interact.core.loan.response.YpGuarNoData;
import com.jy.modules.cims.data.interact.core.loan.response.returnDataList;
import com.jy.modules.cims.interact.CIRLoanIFImpl;
import com.jy.modules.cims.interact.ICInteract;
import com.jy.modules.cims.interact.IInteractAssist;
import com.jy.modules.cims.sysUtils.ImgDtoConverter;
import com.jy.modules.cims.sysUtils.LoanDtoConverter;

/**
 * 额度服务组件默认实现层
 */
@Service("com.jy.modules.cims.component.quota.CQuotaImpl")
public class CQuotaImpl implements ICQuota {
	private static final Logger logger = LoggerFactory.getLogger(CQuotaImpl.class);

	public CQuotaImpl() {
	}

	//@Autowired
	//private TCMmCreditDetailDao creditDetailDao;
	
	/**
	 * 额度数据操作类
	 */
	@Autowired
	public QuotaDao quotaDao;

	@Autowired
	public TCMmContractConstraintDao tcmmContractConstraintDao;

	@Autowired
	TCMmImgurlDao tcmmImgurlDao;
	
	@Autowired
	ImgurlDao imgurlDao;

	@Autowired
	TCMmCooperorgDao tcmmCooperorgDao;

	@Autowired
	private TCMmCreditPackageDao tcmmCreditPackageDao;

	/**
	 * 系统交互处理服务层
	 */
	@Autowired
    @Qualifier("com.jy.modules.cims.interact.CIRLoanIFImpl")
    private CIRLoanIFImpl cIRLoanIFImpl;

	/**
	 * 系统交互处理服务层
	 */
	@Autowired
	@Qualifier("com.jy.modules.cims.interact.CCInteractImpl")
	public ICInteract iCInteract;

	/**
	 * 系统交互处理服务层
	 */
	@Autowired
	@Qualifier("com.jy.modules.cims.component.sysAssist.CSysAssistImpl")
	public ICSysAssist iCSysAssist;

	/**
	 * 客户信息处理服务层
	 */
	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmCustinfoService")
	public TCMmCustinfoService tCMmCustinfoService;

	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmQuotaLoanDetailService")
	public TCMmQuotaLoanDetailService tCMmQuotaLoanDetailService;

	@Autowired
	@Qualifier("com.jy.modules.cims.interact.CInteractAssist")
	public IInteractAssist iInteractAssist;

	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmQuotaRetrunService")
	private TCMmQuotaRetrunService quotaRetrunService;

	@Autowired
	@Qualifier("com.jy.modules.cims.component.loan.CLoanImpl")
	private ICLoan cLoan;

	@Autowired
	@Qualifier("com.jy.modules.cims.component.sysAssist.BorrowersImpl")
	private BorrowersImpl borrowersImpl;

	@Autowired
	@Qualifier("com.jy.modules.cims.component.creditRight.CCreditRightImpl")
	private ICCreditRight iCCreditRight;
	
	@Autowired
	private TCMmContractFeeDao tcmmContractFeeDao;
	

	@Autowired
	private TCMmQuotaCreditDetailDao tcmmQuotaCreditDetailDao;
	

	@Autowired
	@Qualifier("com.jy.modules.cims.component.sysAssist.BatchService")
	public BatchService batchService;
	
	/**
	 * 创建额度申请流水号
	 */
	public String createQuotaCode() {
		return iCSysAssist.getBizNo(CimsConstant.BIZNO_TYPE_QUOTA);
	}

	/**
	 * 分配债权信息给指定交易
	 * 
	 * @param quotaId
	 * @param crTransferId
	 * @param value
	 * @return
	 */
	public void allocCr(String quotaId, String crTransferId, BigDecimal value) {

	}

	/**
	 * 回收被分配出的债权
	 * 
	 * @param quotaId
	 * @param crTransferId
	 * @param value
	 * @return
	 */
	public void recycleCr(String quotaId, String crTransferId, BigDecimal value) {

	}

	/**
	 * 重初始债权
	 * 
	 * @param quotaId
	 * @param crTransferId
	 * @return
	 */
	public void reInitCr(String quotaId, String crTransferId) {

	}

	/**
	 * 分配贷款信息给指定交易
	 * 
	 * @param quotaId
	 * @param loanId
	 * @param value
	 * @return
	 */
	public void allocLoan(String quotaId, String loanId, BigDecimal value) {

	}

	/**
	 * @author chenguixue
	 * @date 2015-1-30 下午2:12:04
	 * @see com.jy.modules.cims.component.quota.ICQuota#getQuotaByCode(String)
	 */
	public QuotaDto getQuotaByCode(String quotaCode) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		QuotaDto dto = new QuotaDto();
		dto.setQuotaCode(quotaCode);
		searchParams.put("dto", dto);
		List<QuotaDto> quotaList = quotaDao.searchQuota(searchParams);
		if (quotaList != null && quotaList.size() > 0)
			return quotaList.get(0);
		return null;
	}

	/**
	 * 回收被分配出的贷款
	 * 
	 * @param quotaId
	 * @param loanId
	 * @param value
	 * @return
	 */
	public void recycleLoan(String quotaId, String loanId, BigDecimal value) {

	}

	/**
	 * 重初始贷款
	 * 
	 * @param quotaId
	 * @param loanId
	 * @return
	 */
	public void reInitLoan(String quotaId, String loanId) {

	}

	/**
	 * 锁定债权使用处理
	 * 
	 * @param quotaId
	 * @param crTransferId
	 * @return
	 */
	public void lockCr(String quotaId, String crTransferId) {

	}

	/**
	 * 锁定贷款使用处理
	 * 
	 * @param quotaId
	 * @param loanId
	 * @return
	 */
	public void lockLoan(String quotaId, String loanId) {

	}

	/**
	 * 获得额度明细列表
	 * 
	 * @param dto
	 * @return
	 */
	public List<Object> getQuotaDetailList(QuotaQueryDto dto) {
		return null;
	}

	/**
	 * 保存额度申请明细
	 * 
	 * @param dto
	 * @return
	 */
	public void saveQuotaList(List<QuotaCreditRightDto> dtolist) throws Exception {
		quotaDao.insertTCMmQuotaCreditDetails(dtolist);
	}

	/**
	 * @author chenguixue
	 * @date 2015-1-29 下午2:28:57
	 * @see com.jy.modules.cims.component.quota.ICQuota#getQuotaList(com.jy.modules.cims.data.dto.QuotaQueryDto)
	 */
	public List<QuotaDto> getQuotaList(QuotaQueryDto dto) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		searchParams.put("dto", dto);
		return quotaDao.searchQuota(searchParams);
	}

	/**
	 * 根据额度申请条件，创建额度申请
	 * 
	 * @param dto
	 *            额度申请条件
	 * @return
	 */
	public JResult saveQuotaApply(QuotaDto dto) {
		JResult result = new JResult();
		Map<String, Object> paramMap = new HashMap<String, Object>();

		String creditPackageName = dto.getCreditPackageName();
		if (SimpleStringUtils.hasText(creditPackageName)) {
			QuotaDto hisQuotaDto = new QuotaDto();
			hisQuotaDto.setCreditPackageName(creditPackageName);
			hisQuotaDto.setValidateState(CimsConstant.DATE_STATUS_VALID);// 有效
			paramMap.put("dto", hisQuotaDto);
			List<QuotaDto> hisQuotaDtoList = quotaDao.searchQuota(paramMap);

			hisQuotaDto = null;
			if (CollectionsUtils.isNotEmpty(hisQuotaDtoList)) {
				String errMsg = java.text.MessageFormat.format("债权包[{0}]已经存在！", creditPackageName);
				result.setErrMsg(errMsg);
				result.setSuccess(false);
				return result;
			}

			// 保存包信息
			TCMmCreditPackageDTO creditPackage = new TCMmCreditPackageDTO();
			creditPackage.setCooperorgCode(dto.getCooperorgCode());// 机构编号
			creditPackage.setContractCode(dto.getContractCode());// 合约编号
			creditPackage.setCreditPackageName(dto.getCreditPackageName());// 包号
			paramMap.clear();
			paramMap.put("dto", creditPackage);
			List<TCMmCreditPackageDTO> creditPackageList = tcmmCreditPackageDao.searchTCMmCreditPackage(paramMap);
			if (CollectionsUtils.isEmpty(creditPackageList)) {
				tcmmCreditPackageDao.insertTCMmCreditPackage(paramMap);
			} else {
				String errMsg = java.text.MessageFormat.format("债权包[{0}]已经存在！", creditPackageName);
				result.setErrMsg(errMsg);
				result.setSuccess(false);
				return result;
			}
			creditPackage = null;
		}

		// 本地保存额度申请信息
		paramMap.clear();
		dto.setQuotaStatus(CimsConstant.QUOTA_STATUS_ST);
		paramMap.put("dto", dto);
		quotaDao.insertQuota(paramMap);

		result.setSuccess(true);
		return result;
	}

	/**
	 * 开启额度申请
	 * 
	 * @param quotaCode
	 *            额度流水号
	 * @return
	 */
	public JResult startWork(String quotaCode) throws Exception {
		QuotaApplyParamDto applyParamDto = new QuotaApplyParamDto();
		QuotaDto quotaDto = new QuotaDto();
		TCMmContractConstraintDTO constraintDTO = new TCMmContractConstraintDTO();
		// 查询额度实体
		Map<String, Object> searchParams = new HashMap<String, Object>();
		quotaDto.setQuotaCode(quotaCode);
		searchParams.put("dto", quotaDto);
		List<QuotaDto> list = quotaDao.searchQuota(searchParams);
		JResult jr = new JResult();
		QuotaApplyResultDto quotaApplyResultDto = null;
		if (CollectionsUtils.isNotEmpty(list)) {
			quotaDto = list.get(0);
			if (CimsConstant.QUOTATYPE_CR.equals(quotaDto.getQuotaType())) {
				applyParamDto.setQuotaType(CimsConstant.QUOTATYPE_CR); // 设置贷款类型
			} else if (CimsConstant.QUOTATYPE_LOAN.equals(quotaDto.getQuotaType())) {
				applyParamDto.setQuotaType(CimsConstant.QUOTATYPE_LOAN); // 设置贷款类型
			}
			TCMmCooperorgDTO tcmmCooperorgDTO = new TCMmCooperorgDTO();
			tcmmCooperorgDTO.setCooperorgCode(quotaDto.getCooperorgCode());
			searchParams.put("dto", tcmmCooperorgDTO);
			List<TCMmCooperorgDTO> cooperOrgList = tcmmCooperorgDao.searchTCMmCooperorg(searchParams);
			if (CollectionsUtils.isNotEmpty(cooperOrgList)) {
				quotaDto.setCooperOrgName(cooperOrgList.get(0).getCooperorgName());
			}
			applyParamDto.setQuotaDto(quotaDto);
			applyParamDto.setDealCode(quotaCode);
			// 查询债权约束
			constraintDTO.setContractCode(quotaDto.getContractCode());
			constraintDTO.setValidateState(CimsConstant.DATE_STATUS_VALID);
			searchParams.put("dto", constraintDTO);
			applyParamDto.setContractConstraintDtoList(tcmmContractConstraintDao.searchTCMmContractConstraint(searchParams));
	
			applyParamDto.setOperType(CimsIrtUrlConstant.IRT_QUOTAAPPLY_OPERTYPE_NEW); // 额度新建
	
			quotaApplyResultDto = iCInteract.quotaApply(applyParamDto);
			
			if (quotaApplyResultDto == null) {
				jr.setSuccess(false);
				jr.setErrMsg("额度申请失败！");
			}
		}
		return jr;
	}

	/**
	 * 删除额度申请
	 * 
	 * @param quotaCode
	 * @return
	 * @throws Exception
	 */
	public void deleteQuota(String quotaCode, List<Object> detailList) throws Exception {
		// 设置额度明细状态为作废状态
		cancelQuotaDetailByQuota(quotaCode, detailList);
		// 设置额度申请为无效状态， 申请状态为释放
		Map<String, Object> searchParams = new HashMap<String, Object>();
		QuotaDto quotaDto = new QuotaDto();
		quotaDto.setQuotaCode(quotaCode);
		quotaDto.setQuotaStatus(CimsConstant.QUOTA_STATUS_UNLOCK);
		quotaDto.setValidateState(CimsConstant.DATE_STATUS_VALID);
		searchParams.put("dto", quotaDto);
		quotaDao.cancelQuota(searchParams);
	}

	/**
	 * @author chenguixue
	 * @throws Exception
	 * @date 2015-1-27 下午5:56:48
	 * @see com.jy.modules.cims.component.quota.ICQuota#saveLoanFromInteract(java.util.List)
	 */
	public void saveLoanFromInteract(List<LoanInfo> loanInfoDataList, String sysCode) throws Exception {
		if (CollectionsUtils.isEmpty(loanInfoDataList)) {
			return;
		}
		
		// 循环迭代保存贷款相关信息
		for (LoanInfo loanInfo : loanInfoDataList) {
			saveLoanFromInteract(loanInfo,  sysCode);
		}
	}
	
	public void saveLoanFromInteract(LoanInfo loanInfo, String sysCode) throws Exception {
		
		TCMmQuotaLoanDetailDTO loanDetail = null;
		TCMmCustinfoDTO custinfo = null;
		LoanDtoConverter loanConverter = LoanDtoConverter.getInstance();

		String contractNo = loanInfo.getContractNo();// 合同号
		
		// 从贷前系统获取的信息
		LoanData loanData = iCInteract.getBefLoanInfo(contractNo);

		if(CimsConstant.TBJ_CHECK_RAISE){
		   // 保存借款人相关方面的信息
		   savaBorrowersResult(contractNo, loanInfo.getCustCode());
		}
		
		try {
			loanDetail = loanConverter.convert(loanInfo);
		} catch (ParseException e) {
			logger.info(e.getMessage());
		}

		if (null == loanDetail) {
			return ;
		}
		
		String productType = productTypeMap(loanInfo.getLoanType());
		if (null != loanData) {
			loanDetail.setRepayAcctNo(loanData.getRepayAcctNo());// 还款账号
			loanDetail.setRepayAcctName(loanData.getRepayAcctName());// 还款账户名称
			loanDetail.setRepayBankCode(loanData.getRepayBankCode());// 还款行代码
			loanDetail.setBankbranchName(loanData.getBankBranchName());// 放款行支行名称
			
			//String productType = getProdNameToprodType(loanData.getProductName());
			loanDetail.setProductType(productType);//产品类型
			
			loanDetail.setMonthCompositeRate(loanData.getMonthCompositeRate()); // 贷款月综合利率
			loanDetail.setRepaymentDateCalculation(loanData.getRepaymentDateCalculation());//还款日计算方式
			
			loanDetail.setManagementStore(loanData.getManagementStore());//管理门店 
			loanDetail.setAcceptanceStores(loanData.getAcceptanceStores());//受理门店 
			loanDetail.setBookingLoanDate(loanData.getBookingLoanDate());//预约还款日
		}

		loanDetail.setDealStatus(CimsConstant.QUOTA_DETAIL_STATUS_NEW);// 申请
		// 新增贷款额度明细
		Long loanId = tCMmQuotaLoanDetailService.insertTCMmQuotaLoanDetail(loanDetail);

		custinfo = new TCMmCustinfoDTO();
		SimpleBeanUtils.copyBean2Bean(custinfo, loanInfo);
		
		if(SimpleStringUtils.hasText(loanInfo.getBirthDate())){
			try{
			  custinfo.setBirthDate(DateTimeUtils.parse(loanInfo.getBirthDate(), "yyyy-MM-dd"));
			}catch(Exception e){
				logger.info(e.getMessage());
			}
		}
		
		custinfo.setFromType(CimsConstant.QUOTATYPE_LOAN);
		custinfo.setFromCode(loanInfo.getContractNo());

		// 保存客户信息
		tCMmCustinfoService.insertTCMmCustinfo(custinfo);

		//保存影像信息
		saveImageInfo(loanData, loanInfo.getContractNo(), loanId, productType);
		
		//保存合同信息
		saveFeesData(contractNo, loanInfo.getFeesDataList());
		
		// 保存还款计划
	   /*	List<LoanSchedule> planList = loan.getPlantDataList();
		Map<String, String> map = new HashMap<String, String>();
		map.put("creditcode", contractNo);
		map.put("transferid", contractNo);
		int count = repaymentPlanDao.selectCountLoanPlans(map);
		if (count == 0) {
			repaymentPlanDao.insertLoanPlanList(planList);
		}*/
	
	   loanDetail = null;
	}
	
	//接口合同类型与产品类型对应关系
	private String productTypeMap(String loanType){
		
		if(loanType.equals(CimsConstant.CORE_PRODUCT_TYPE_CREDIT)){
			return CimsConstant.PRODUCT_TYPE_CREDIT;
		}else if(loanType.equals(CimsConstant.CORE_PRODUCT_TYPE_HOUSE)){
			return CimsConstant.PRODUCT_TYPE_HOUSE;
		}else if(loanType.equals(CimsConstant.CORE_PRODUCT_TYPE_CAR_YJ)||loanType.equals(CimsConstant.CORE_PRODUCT_TYPE_CAR_GPS)){
			return CimsConstant.PRODUCT_TYPE_CAR;
		}else{
			return CimsConstant.PRODUCT_TYPE_NOT_EXIST;
		}
		
	}
	
	/**
	 * 保存债权影像信息
	 * @author zhoupeng  TODO
	 * @date 2015-11-14 上午10:41:33
	 * @param dto
	 */
	public void saveImageInfo(LoanData loanData, String contractNo, Long loanId, String productType) throws Exception{
		if (null == loanData || !SimpleStringUtils.hasText(contractNo)) {
			return;
		}
		
		ImgurlDto imgurlDto = new ImgurlDto();
		imgurlDto.setContractNo(contractNo);
		Long imgurlRows = imgurlDao.getImgurlRows(SearchParamUtil.getSearchParam(imgurlDto));
		if(imgurlRows.intValue() > 0){
			logger.info("合同号["+contractNo+"]已经存在影像文件不需要新增影像文件！");
			return;
		}
		
		List<ImgUrl> imgList = new ArrayList<ImgUrl>();
		ImgDtoConverter imgConverter = ImgDtoConverter.getInstance();
		//Map<String, Object> paramMap = new HashMap<String, Object>();
		
		String custNo = loanData.getCustCode();
		List<UrlData> urlDataList = loanData.getUrlDataList();//信用贷影像文件列表
		List<YpGuarNoData> ypGuarNoList = loanData.getYpGuarNoList();// 押品编号列表
		
		if (CollectionsUtils.isNotEmpty(urlDataList)) {
			ImgUrl imgUrl = null;
			for (UrlData urlData : urlDataList) {
				imgUrl = new ImgUrl();
				imgUrl.setAttachCode(urlData.getAttachCode());
				imgUrl.setAttachName(urlData.getAttachName());
				imgUrl.setAttachUrl(urlData.getAttachUrl());
				imgUrl.setCustNo(custNo);//客户编号
				imgUrl.setContractNo(contractNo);
				imgUrl.setProductType(productType);//产品类型
				imgUrl.setProductCode(loanData.getProductCode());
				imgList.add(imgUrl);
				imgUrl = null;
			}
		}
		
		List<String> collateralDataList = new ArrayList<String>();
		if (CollectionsUtils.isNotEmpty(ypGuarNoList)) {
			for (YpGuarNoData ypGuarNo : ypGuarNoList) {
				collateralDataList.add(ypGuarNo.getGuarNo());
			}
		}
	    if(CollectionsUtils.isNotEmpty(collateralDataList)){
	    	//根据押品编号 获取押品系统附件表信息
			Response2011DTO collaterlist = cIRLoanIFImpl.getBefCollateralResult(collateralDataList);
			if (null != collaterlist) {
				List<returnDataList> dataList = collaterlist.getReturnDataList();
				if (CollectionsUtils.isNotEmpty(dataList)) {
					ImgUrl imgUrl = null;
					// 获取贷款状态 
					for (returnDataList data : dataList) {
						imgUrl = new ImgUrl();
						imgUrl.setAttachCode(data.getFiletypecode());//文档类型
						imgUrl.setAttachName(data.getFilename());// 影像类型  A1
						imgUrl.setAttachUrl(data.getFilepath());//文件地址
						imgUrl.setCustNo(custNo);//客户编号
						imgUrl.setContractNo(contractNo);//合同编号
						imgUrl.setGuarNo(data.getGuarno());//押品编号
						imgUrl.setProductType(productType);
						imgUrl.setProductCode(loanData.getProductCode());
						imgList.add(imgUrl);
						imgUrl = null;
					}
				}
			}
		}
			
		if (CollectionsUtils.isNotEmpty(imgList)) {
			List<TCMmImgurlDTO> imgurlDtoList = new ArrayList<TCMmImgurlDTO>();
			// 保存影像
			for (ImgUrl imgUrl : imgList) {
				TCMmImgurlDTO imgDto = imgConverter.convert(imgUrl);
				imgDto.setCustNo(custNo);
				imgDto.setLoanId(loanId);
				imgurlDtoList.add(imgDto);
				/*
				paramMap.clear();
				paramMap.put("dto", imgDto);
				tcmmImgurlDao.insertTCMmImgurl(paramMap);*/
			}
			
			imgurlDao.insertImgurlByBatch(imgurlDtoList);
		}
	}
	
	/**
	 * 
	 * @Description 保存合同费用
	 * @param contractNo
	 * @param feesDataList   
	 * void  
	 * @throws
	 * @author zhangmi
	 * @date 2016-4-7
	 */
	public void saveFeesData(String contractNo,List<FeesData> feesDataList) throws Exception{
		TCMmContractFeeDTO feeDto = null;
		TCMmContractFeeDTO searchfeeDto = null;
		Map<String, Object> paramMap = null;
		if(CollectionsUtils.isNotEmpty(feesDataList)){
			for (FeesData feesData : feesDataList) {
				searchfeeDto = new TCMmContractFeeDTO();
				searchfeeDto.setContractNo(contractNo);
				searchfeeDto.setFeesCode(feesData.getFeesCode());
				List<TCMmContractFeeDTO> list = tcmmContractFeeDao.searchTCMmContractFee(SearchParamUtil.getSearchParam(searchfeeDto));
				if(CollectionsUtils.isEmpty(list)){
					feeDto = new TCMmContractFeeDTO();
					paramMap = new HashMap<String, Object>();
					feeDto.setContractNo(contractNo);
					feeDto.setFeesRate(feesData.getFeesRate());
					feeDto.setFeesName(feesData.getFeesName());
					feeDto.setFeesCode(feesData.getFeesCode());
					feeDto.setFeesAmount(feesData.getFeesAmount());
					feeDto.setCreateTime(new Timestamp(System.currentTimeMillis()));
					feeDto.setCreateBy(feeDto.getCreateUserNameExt());
					paramMap.put("dto", feeDto);
					tcmmContractFeeDao.insertTCMmContractFee(paramMap);
				}
			}
		}
	}
	
	/**
	 * 根据值转换为贷款状态 '贷款类型:01-信用贷;02-车贷;03-房贷;';
	 * 
	 * @author zhoupeng  
	 * @date 2015-11-16 上午10:41:33
	 */
	public static String getProdNameToprodType(String prodName)
	{
		logger.info("贷款prodName::" + prodName);
		prodName = prodName==null?"":prodName;
		
		String prodType=null;
		if(prodName.contains("房贷")){
			prodType= CimsConstant.PRODUCT_TYPE_HOUSE;
		}else if(prodName.contains("车贷")){
			prodType= CimsConstant.PRODUCT_TYPE_CAR;
		}else{
			prodType= CimsConstant.PRODUCT_TYPE_CREDIT;
		}
		
		logger.info("贷款prodType::" + prodType);
		return prodType;
	}
	
	/**
	 * 批量根据申请流水号更新的额度申请信息的额度申请状态：完成
	 * 
	 * @author chenguixue
	 * @date 2015-1-29 上午10:41:33
	 * @see com.jy.modules.cims.component.quota.ICQuota#endQuota(java.util.List)
	 */
	@Override
	public void endQuota(List<String> quotaCodeList) {
		if (null == quotaCodeList || quotaCodeList.size() == 0) {
			return;
		}

		Map<String, Object> paramMap = null;
		QuotaDto quotaDto = null;

		for (String quotaCode : quotaCodeList) {
			paramMap = new HashMap<String, Object>();
			quotaDto = new QuotaDto();

			quotaDto.setQuotaCode(quotaCode);
			quotaDto.setQuotaStatus(CimsConstant.QUOTA_STATUS_ED);
			paramMap.put("dto", quotaDto);

			quotaDao.updateValidateStateByQuotaCode(paramMap);
		}

		quotaDto = null;
		paramMap = null;
	}

	/**
	 * 更新小于失效日期的额度申请信息的额度申请状态：完成
	 * 
	 * @author chenguixue
	 * @date 2015-1-29 上午10:42:30
	 * @see com.jy.modules.cims.component.quota.ICQuota#endQuota(java.util.Date)
	 */
	@Override
	public void endQuota(Date endDate) {

		Map<String, Object> paramMap = new HashMap<String, Object>();
		QuotaDto quotaDto = new QuotaDto();
		quotaDto.setInvalidDate(endDate);
		quotaDto.setQuotaStatus(CimsConstant.QUOTA_STATUS_ED);
		paramMap.put("dto", quotaDto);

		quotaDao.updateValidateStateByInvalidDate(paramMap);

		quotaDto = null;
		paramMap = null;
	}

	/**
	 * @author chenguixue
	 * @date 2015-1-30 下午2:05:28
	 * @see com.jy.modules.cims.component.quota.ICQuota#cancelQuotaDetailByQuota(java.lang.String)
	 */
	@Override
	public List<Object> cancelQuotaDetailByQuota(String quotaCode, List<Object> detailList) {
		QuotaDto quotaDto = getQuotaByCode(quotaCode);
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, Object> dto = new HashMap<String, Object>();
		dto.put("quotaCode", quotaCode);
		dto.put("invalidStatus", CimsConstant.QUOTA_DETAIL_STATUS_INVALID);
		dto.put("newStatus", CimsConstant.QUOTA_DETAIL_STATUS_NEW);
		searchParams.put("dto", dto);
		
		if (CimsConstant.QUOTATYPE_LOAN.equals(quotaDto.getQuotaType())) { // 作废贷款
			List<TCMmQuotaLoanDetailDTO> LoanList = quotaDao.findCancelQuotaLoanDetailByQuota(searchParams);
			if (LoanList != null) {
				for (TCMmQuotaLoanDetailDTO loandto : LoanList) {
					detailList.add(loandto);
				}
			}
			quotaDao.cancelQuotaLoanDetailByQuota(searchParams);
		}
		if (CimsConstant.QUOTATYPE_CR.equals(quotaDto.getQuotaType())) { // 作废债权
			List<TCMmQuotaCreditDetailDTO> creditList = quotaDao.findCancelQuotaCRDetailByQuota(searchParams);
			if (creditList != null) {
				for (TCMmQuotaCreditDetailDTO crdto : creditList) {
					detailList.add(crdto);
				}
			}
			quotaDao.cancelQuotaCRDetailByQuota(searchParams);
		}
		return detailList;
	}
	
	/**
	 * 根据合同编号作废额度明细
	 */
	@Override
	public List<Object> cancelQuotaDetailByQuotaDetail(String contractNo, List<Object> detailList,String riskTimeString) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, Object> dto = new HashMap<String, Object>();
		dto.put("contractNo", contractNo);
		dto.put("invalidStatus", CimsConstant.QUOTA_DETAIL_STATUS_INVALID);
		dto.put("newStatus", CimsConstant.QUOTA_DETAIL_STATUS_NEW);
		dto.put("riskTimeString", riskTimeString);
		searchParams.put("dto", dto);

		TCMmQuotaLoanDetailDTO loandto = new TCMmQuotaLoanDetailDTO();
		loandto.setContractNo(contractNo);
		detailList.add(loandto);
		
		quotaDao.cancelQuotaLoanDetailByContractNo(searchParams);
		
		return detailList;
	}

	/**
	 * @author chenguixue
	 * @date 2015-1-30 下午2:05:37
	 * @see com.jy.modules.cims.component.quota.ICQuota#cancelQuotaDetailByList(java.lang.String,
	 *      java.util.List)
	 */
	public void cancelQuotaDetailByList(String quotaType, List<String> detailCode) {
		return;
	}

	/**
	 * 
	 * @author lizhiqiang
	 * @date 2015-8-13 下午8:03:53
	 * @see com.jy.modules.cims.component.quota.ICQuota#searchQuotaCreditDetailByPaging(java.util.Map)
	 */
	public List<QuotaCreditDetailDto> searchQuotaCreditDetailByPaging(Map<String, Object> searchParams) {
		return quotaDao.searchQuotaCreditDetailByPaging(searchParams);
	}

	/**
	 * 
	 * @author lizhiqiang
	 * @date 2015-8-13 下午8:03:35
	 * @see com.jy.modules.cims.component.quota.ICQuota#searchQuotaCreditDetail(java.util.Map)
	 */
	public List<QuotaCreditDetailDto> searchQuotaCreditDetail(Map<String, Object> searchParams) {
		return quotaDao.searchQuotaCreditDetail(searchParams);
	}

	/**
	 * 
	 * @author lizhiqiang
	 * @date 2015-8-13 下午8:35:52
	 * @see com.jy.modules.cims.component.quota.ICQuota#searchTotalQuotaCreditDetail(java.util.Map)
	 */
	public BigDecimal searchTotalQuotaCreditDetail(Map<String, Object> searchParams) {
		return quotaDao.searchTotalQuotaCreditDetail(searchParams);
	}

	/**
	 * 单笔执行。
	 * 
	 * @Title doSingleGetCreditRightQuotaApplyResult
	 * @Description TODO
	 * @param tcMmQuotaDTO
	 * @throws Exception
	 *             void
	 * @author lizhiqiang
	 * @date 2015-5-14-下午4:21:30
	 * @update
	 * @throws
	 */
	public void doSingleGetCreditRightQuotaApplyResult(QuotaDto tcMmQuotaDTO) throws Exception {
//		Map<String, Object> searchParams = new HashMap<String, Object>();
//		TCMmQuotaRetrunDTO quotaRetrunDTO = null;
//		quotaRetrunDTO = new TCMmQuotaRetrunDTO();
//		quotaRetrunDTO.setQuotaCode(tcMmQuotaDTO.getQuotaCode());
//		searchParams.put("dto", quotaRetrunDTO);
		//List<TCMmQuotaRetrunDTO> quotaRetrunDTOList = quotaRetrunService.searchTCMmQuotaRetrun(searchParams);

		QuotaCreditRightDto searchParamsdto = new QuotaCreditRightDto(); 
		TCMmQuotaDTO tcmQto = new TCMmQuotaDTO();
		searchParamsdto.settCMmQuotaDTO(tcmQto);
		searchParamsdto.setQuotaCode(tcMmQuotaDTO.getQuotaCode());
		List<QuotaCreditDetailDto> quotaCreditListInserted = quotaDao.searchQuotaCreditDetail(SearchParamUtil.getSearchParam(searchParamsdto));
		
		QuotaApplyParamDto quotaApplyParamDto = null;
		int curPage = 1;//第一页
		int pageSize = 50;//每页50条
		int totalRows = 0;//默认0条
		int lastCurPage = 1;//总共几页
		QuotaApplyResultDto quotaApplyResultDto = null;
		for (curPage = 1; curPage <= lastCurPage; curPage++) {
			String dealCode = iInteractAssist.getBusiNoForInteract();
			iInteractAssist.registerDealIrtService(dealCode);
			quotaApplyParamDto = new QuotaApplyParamDto();// 创建参数对象
			quotaApplyParamDto.setQuotaType(CimsConstant.QUOTATYPE_CR);
			quotaApplyParamDto.setDealCode(dealCode);
			quotaApplyParamDto.setQuotaDto(tcMmQuotaDTO);
			//quotaApplyParamDto.setQuotaRetrunDTOList(quotaRetrunDTOList);
			quotaApplyParamDto.setOperType(CimsIrtUrlConstant.IRT_QUOTAAPPLY_OPERTYPE_QUERY);
			
			quotaApplyParamDto.setCurPage(curPage);
			quotaApplyParamDto.setPageSize(pageSize);
			quotaApplyParamDto.setTotalRows(totalRows);
			
			quotaApplyResultDto = iCInteract.getQuotaApplyResult(quotaApplyParamDto);
			if (quotaApplyResultDto != null && quotaApplyResultDto.isSuccess()) {

				totalRows = quotaApplyResultDto.getTotalRows();//获取总行数
				lastCurPage = totalRows%pageSize==0?(totalRows/pageSize):(totalRows/pageSize+1);//计算最后页数
				
				List<QuotaCreditRightDto> crList = quotaApplyResultDto.getCrList();
				List<CustInfoDto> custInfoList = quotaApplyResultDto.getCustInfoList();
				List<QuotaCreditRightDto> quotaCreditRightDtoList = new ArrayList<QuotaCreditRightDto>();
				if (crList != null && crList.size() > 0) {
					Map<String, String> batchMap = new HashMap<String, String>();
					for (QuotaCreditRightDto  dto: crList) {
						
						//判断重复问题
						boolean isInserted = false;
						for(QuotaCreditDetailDto qtDto :quotaCreditListInserted){
							if(dto.getTransferId().equals(qtDto.getTransferId())
									&&dto.getCreditCode().equals(qtDto.getCreditCode())){
								for (int i = 0; i < custInfoList.size(); i++) {
									if(dto.getTransferId().equals(custInfoList.get(i).getFromCode())){
										custInfoList.remove(i);
									}
								}
								isInserted = true;
								break;
							}
						}
						if(isInserted){
							continue;
						}
						
						logger.info("LoanProductName:::::"+ dto.getLoanProductName());
						dto.setDealStatus(CimsConstant.QUOTA_DETAIL_STATUS_NEW); // 初始状态
						dto.setCreditorValueUpdateDate(new Date());//债权更新日期
						

						String productType = dto.getProductType();
//						dto.setProductType(productType);//产品类型
						
						if (!batchMap.containsKey(dto.getBatchId())) {
							if (null != dto.getBatchId())
								batchMap.put(dto.getBatchId(), "");
						}
						String contractCode = dto.getCreditCode();
						logger.info("债权编号/合同号：" + contractCode);
						TCMmCreditDetailDTO srCreditRight = new TCMmCreditDetailDTO();
						boolean isSRCreditRight = iCCreditRight.isSRCreditRight(contractCode, srCreditRight);
						logger.info(isSRCreditRight + "(true:是受让债权； false:不是受让债权)");
						// 不是受让债权从贷前系统取以下信息
						if(isSRCreditRight==false){
						 LoanData loanData = iCInteract.getBefLoanInfo(contractCode);
						 if (null != loanData) {
							dto.setRepayAcctNo(loanData.getRepayAcctNo());// 还款账号
							dto.setRepayAcctName(loanData.getRepayAcctName());// 还款账户名称
							dto.setRepayBankCode(loanData.getRepayBankCode());// 还款行代码
							dto.setBankBranchName(loanData.getBankBranchName());// 放款行支行名称
							//dto.setRepayType(loanData.getRepayType());// 还款方式(2016-02-29 修改为从核心获取还款方式)
			
							dto.setMonthCompositeRate(loanData.getMonthCompositeRate()); // 贷款月综合利率
							dto.setRepaymentDateCalculation(loanData.getRepaymentDateCalculation());//还款日计算方式
							
							dto.setManagementStore(loanData.getManagementStore());//管理门店 
							dto.setAcceptanceStores(loanData.getAcceptanceStores());//受理门店 
							dto.setBookingLoanDate(loanData.getBookingLoanDate());//预约放款日
							//保存债权影像信息
							saveImageInfo(loanData, contractCode, null, productType);
							//保存债权对应的借款人信息
							savaBorrowersResult(contractCode, loanData.getCustCode());
						 }
						}
						// 受让债权再次债权转让，从合作机构本地获取还款方式和产品类型
						//else{
							//srCreditRight = new TCMmCreditDetailDTO();
							//srCreditRight.setCreditCode(contractCode);
							//srCreditRight.setCreditSource(CimsConstant.CREDIT_SOURCE_REC);//来自债权受让
							//List<TCMmCreditDetailDTO> creditDetailList = creditDetailDao.searchTCMmCreditDetail(SearchParamUtil.getSearchParam(srCreditRight));
							//if(CollectionsUtils.isNotEmpty(creditDetailList)){
								//dto.setRepayType(creditDetailList.get(0).getRepayType());// 还款方式
//								String productType = getProdNameToprodType(creditDetailList.get(0).getLoanProductName());
//								dto.setProductType(productType);//产品类型
							//}
						//}
						quotaCreditRightDtoList.add(dto);
					}
					Iterator<?> iterator = batchMap.entrySet().iterator();
					while (iterator.hasNext()) {
						Entry<?, ?> entry = (Entry<?, ?>) iterator.next();
						Object key = entry.getKey();
						TCMmQuotaRetrunDTO qrDto = new TCMmQuotaRetrunDTO();
						qrDto.setBatchId((String) key);
						qrDto.setQuotaCode(tcMmQuotaDTO.getQuotaCode());
						qrDto.setQuotaType(CimsConstant.QUOTATYPE_CR);
						quotaRetrunService.insertTCMmQuotaRetrun(qrDto);
					}
					
					//quotaDao.insertTCMmQuotaCreditDetails(quotaCreditRightDtoList);
					//quotaDao.insertCustInfoDetails(custInfoList);

					// 分批处理
					if(CollectionsUtils.isNotEmpty(quotaCreditRightDtoList)){
						batchService.batchInvoke(CQuotaImpl.class, "insertTCMmQuotaCreditDetails", quotaCreditRightDtoList, null, 50);
						batchService.batchInvoke(CQuotaImpl.class, "insertCustInfoDetails", custInfoList, null, 50);
					}
				}
			}
			quotaApplyResultDto = null;
		}
		
		
		
		
	}
	
	/**
	 * 
	 * @param quotaCreditRightDtoList
	 */
	public void insertTCMmQuotaCreditDetails(List<QuotaCreditRightDto> quotaCreditRightDtoList){
		quotaDao.insertTCMmQuotaCreditDetails(quotaCreditRightDtoList);
	}
	 
	/**
	 * 
	 * @param custInfoList
	 */
	public void insertCustInfoDetails(List<CustInfoDto> custInfoList){
		quotaDao.insertCustInfoDetails(custInfoList);
	}
	
	/**
	 * @description 新增贷款申请信息
	 * @param quotaDto
	 * @throws Exception
	 */
	public void addLoantQuotaApply(QuotaDto quotaDto) throws Exception {
		if (null == quotaDto) {
			return;
		}

		try {
			String dealCode = iInteractAssist.getBusiNoForInteract();
			iInteractAssist.registerDealIrtService(dealCode);

			QuotaApplyParamDto paramDto = new QuotaApplyParamDto();
			paramDto.setPageSize(CimsConstant.DEFAULT_PAGE_SIZE);
			paramDto.setCurPage(1);
			paramDto.setQuotaDto(quotaDto);
			paramDto.setQuotaType(CimsConstant.QUOTATYPE_LOAN);
			paramDto.setDealCode(dealCode);

			// 获取数据
			QuotaApplyResultDto resultDto = iCInteract.getQuotaApplyResult(paramDto);
			if (resultDto == null) {
				return;
			}

			// 计算分页
			int totalCount = resultDto.getTotalRows();
			if (totalCount <= 0) {
				return;
			}

			int pageSum = totalCount / CimsConstant.DEFAULT_PAGE_SIZE;
			pageSum = (totalCount % CimsConstant.DEFAULT_PAGE_SIZE == 0) ? pageSum : pageSum + 1;

			// 保存当页数据
			List<LoanInfo> loanInfoList = getNotExistLoanInfoList(quotaDto.getQuotaCode(), resultDto.getLoanList());
			saveLoanFromInteract(loanInfoList, null);

			// 重复执行后续页
			for (int curPage = 2; curPage < pageSum + 1; curPage++) {
				paramDto.setCurPage(curPage);
				resultDto = iCInteract.getQuotaApplyResult(paramDto);
				loanInfoList = getNotExistLoanInfoList(quotaDto.getQuotaCode(), resultDto.getLoanList());
				saveLoanFromInteract(loanInfoList, null);
			}

			paramDto = null;

		} catch (Exception e) {
			logger.info(e.getMessage());
		}
	}

	/**
	 * @description 额度取消
	 * @param quotaDto
	 */
	public void quotaCancel(QuotaDto quotaDto) {
		String busiInteractCode = iInteractAssist.getBusiNoForInteract();
		iInteractAssist.registerDealIrtService(busiInteractCode.toString());

		// 调用额度申请取消接口
		QuotaApplyParamDto quotaApplyParamDto = new QuotaApplyParamDto();
		quotaApplyParamDto.setDealCode(busiInteractCode);
		quotaApplyParamDto.setQuotaType(quotaDto.getQuotaType());
		quotaApplyParamDto.setQuotaDto(quotaDto);
		quotaApplyParamDto.setOperType(CimsIrtUrlConstant.IRT_QUOTAAPPLY_OPERTYPE_CANCEL); // 额度取消

		TCMmCooperorgDTO tcmmCooperorgDTO = new TCMmCooperorgDTO();
		tcmmCooperorgDTO.setCooperorgCode(quotaDto.getCooperorgCode());
		List<TCMmCooperorgDTO> cooperOrgList = tcmmCooperorgDao.searchTCMmCooperorg(SearchParamUtil.getSearchParam(tcmmCooperorgDTO));
		if (CollectionsUtils.isNotEmpty(cooperOrgList)) {
			quotaDto.setCooperOrgName(cooperOrgList.get(0).getCooperorgName());
		}

		iCInteract.quotaCancel(quotaApplyParamDto);
	}

	/**
	 * @description 额度明细释放
	 * @param quotaDto
	 * @param detailList
	 */
	public void quotaDetailUnlock(QuotaDto quotaDto, List<Object> detailList) {
		if (null == quotaDto || CollectionsUtils.isEmpty(detailList)) {
			return;
		}

		String busiInteractCode = iInteractAssist.getBusiNoForInteract();
		iInteractAssist.registerDealIrtService(busiInteractCode.toString());

		QuotaDetailUnlockParamDto quotaDetailUnlockParamDto = new QuotaDetailUnlockParamDto();
		quotaDetailUnlockParamDto.setDealCode(busiInteractCode);
		quotaDetailUnlockParamDto.setQuotaType(quotaDto.getQuotaType());
		List<QuotaDetailUnlockParamDto.LoanUnlockInfo> unlockList = new ArrayList<QuotaDetailUnlockParamDto.LoanUnlockInfo>();
		QuotaDetailUnlockParamDto.LoanUnlockInfo loanUnlockDto = null;

		for (Object obj : detailList) {
			TCMmQuotaLoanDetailDTO loan = (TCMmQuotaLoanDetailDTO) obj;
			loanUnlockDto = new QuotaDetailUnlockParamDto.LoanUnlockInfo();
			unlockList.add(loanUnlockDto);
			loanUnlockDto.setContractNo(loan.getContractNo());
			loanUnlockDto.setUnlockType(CimsConstant.LOAN_OPER_TYPE_CC);
			loanUnlockDto.setUnlockReason("");
		}
		quotaDetailUnlockParamDto.setUnlockList(unlockList);

		iCInteract.quotaDetailUnlock(quotaDetailUnlockParamDto);
	}

	/**
	 * @description 获取数据不存在贷款信息表的记录
	 * @param quotaCode
	 *            额度申请编号
	 * @param LoanInfoList
	 *            筛选前没处理的贷款信息
	 * @return 处理后的贷款信息，在数据库表已经存在的需要过滤掉
	 */
	public List<LoanInfo> getNotExistLoanInfoList(final String quotaCode, final List<LoanInfo> loanInfoList) {
		if (CollectionsUtils.isEmpty(loanInfoList)) {
			return null;
		}

		List<String> contractNoList = new ArrayList<String>();
		for (LoanInfo loanInfo : loanInfoList) {
			contractNoList.add(loanInfo.getContractNo());
		}

		List<TCMmQuotaLoanDetailDTO> quotaLoanDetailList = cLoan.getQuotaLoanDetailByQuotaCodeAndContractNoList(quotaCode, contractNoList);
		contractNoList = null;
		if (CollectionsUtils.isEmpty(quotaLoanDetailList)) {
			return loanInfoList;
		}

		List<LoanInfo> result = new ArrayList<LoanInfo>();
		for (LoanInfo loanInfo : loanInfoList) {
			String contractNo = loanInfo.getContractNo().trim();
			boolean exist = false;
			for (TCMmQuotaLoanDetailDTO quotaLoanDetail : quotaLoanDetailList) {
				if (contractNo.equals(quotaLoanDetail.getContractNo().trim())) {
					exist = true;
					continue;
				}
			}

			if (!exist) {
				result.add(loanInfo);
			}
		}

		return result;
	}

	
	public void savaBorrowersResult(final String contractCode, final String custCode) throws Exception {
		//获取债权对应的借款人信息
		Response8003DTO response8003DTO = iCInteract.getBorrowersResult(contractCode);
		savaBorrowersResult(response8003DTO, custCode);
	}
	
	/**
	 * @description 保存借款人相关方面的信息
	 * @param response8003DTO
	 * @throws Exception
	 */
	public void savaBorrowersResult(final Response8003DTO response8003DTO, final String custCode) throws Exception {
		logger.info("保存借款人相关方面的信息==>>");
		if (null == response8003DTO) {
			return;
		}
		
		String contractNo = response8003DTO.getContractNo();
		BorrowerDTO borrower = response8003DTO.getBorrower();// 借款人基本信息
		List<BorrowerBankCardDTO> bankCardList = response8003DTO.getBankCard();// 借款人银行卡信息
		List<BorrowerCreditInfoDTO> creditInfoList = response8003DTO.getCreditInfo();// 借款人房贷信息
		List<BorrowerLoandebtDTO> loandebtList = response8003DTO.getLoandebt();// 借款人征信信息
		List<BorrowerBizEntityDTO> entityList = response8003DTO.getEntity();// 借款人经营实体明细
		List<BorrowerInsuranceDTO> insuranceList = response8003DTO.getInsurance();// 借款人投保信息
		BorrowerJobsDTO jobs = response8003DTO.getJobs();// 借款人工作信息
		BorrowerThirdDataDTO thirdData = response8003DTO.getThirdData();// 借款人第三方测试数据
		List<BorrowerAttenDTO> contactInfoList = response8003DTO.getContactInfo();// 借款人联系人信息
		List<BorrowerAssetDTO> assetList = response8003DTO.getAsset();// 借款人联系人信息

		// 保存借款人基本信息
		borrowersImpl.insertTCMmBorrower(borrower, contractNo, custCode);
		// 保存借款人银行卡信息
		borrowersImpl.insertTCMmBorrowerBankCard(bankCardList, contractNo, custCode);	
		//借款人房贷信息
		borrowersImpl.insertTCMmBorrowerCreditInfo(creditInfoList, contractNo, custCode);	
		//借款人征信信息
		borrowersImpl.insertTCMmBorrowerLoandebt(loandebtList, contractNo, custCode);		
		// 保存借款人经营实体明细
		borrowersImpl.insertTCMmBorrowerBizEntity(entityList, contractNo, custCode);		
		// 保存借款人投保信息
		borrowersImpl.insertTCMmBorrowerInsurance(insuranceList, contractNo, custCode);	
		// 保存借款人工作信息
		borrowersImpl.insertTCMmBorrowerJobs(jobs, contractNo, custCode);
		// 保存借款人第三方测试数据
		borrowersImpl.insertTCMmBorrowerThirdData(thirdData, contractNo, custCode);
		// 保存借款人联系人信息
		borrowersImpl.insertTCMmBorrowerAtten(contactInfoList, contractNo, custCode);
		// 保存借款人房产信息
		borrowersImpl.insertTCMmBorrowerAsset(assetList, contractNo, custCode);
	}
	
	/**
	 * 根据额度申请编号，更新额度申请状态
	 * 
	 * @param quotaCode 额度申请编号
	 * 
	 */
	public void updateQuotaStatusByQuotaCode(String quotaCode) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		QuotaDto dto = new QuotaDto();
		dto.setQuotaCode(quotaCode);
		dto.setQuotaStatus(CimsConstant.QUOTA_STATUS_ST);
		paramMap.put("dto", dto);
		quotaDao.updateQuotaStatusByQuotaCode(paramMap);
	}
	
	/**
	 * 
	 * @author zhangmi
	 * @date  2017年3月9日
	 * @param quotaCode 额度编号
	 * @param quotaStatus 修改的额度状态
	 */
	public void updateCancelQuotaStatusByQuotaCode(String quotaCode,String quotaStatus) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		QuotaDto dto = new QuotaDto();
		dto.setQuotaCode(quotaCode);
		dto.setQuotaStatus(quotaStatus);
		paramMap.put("dto", dto);
		quotaDao.updateQuotaStatusByQuotaCode(paramMap);
	}
	/**
	 * 查询额度债权明细
	 * @author zhangmi
	 * @date  2017年3月10日
	 * @param searchParams
	 * @return
	 */
	public List<TCMmQuotaCreditDetailDTO> searchTCMmQuotaCreditDetail(Map<String, Object> searchParams) {
		return tcmmQuotaCreditDetailDao.searchTCMmQuotaCreditDetail(searchParams);
	}
	/**
	 * 
	 * @author zhangmi
	 * @date  2017年3月10日
	 * @param quotaCode
	 * @param oldDealStatus
	 */
	public void updateCncelQuotaCRDetailByQuota(String quotaCode,String oldDealStatus) {
		Map<String, Object> dto = new HashMap<String, Object>();
		dto.put("quotaCode", quotaCode);
		dto.put("invalidStatus", CimsConstant.QUOTA_DETAIL_STATUS_INVALID);
		dto.put("newStatus", oldDealStatus);
		quotaDao.cancelQuotaCRDetailByQuota(SearchParamUtil.getSearchParam(dto));
	}
}